$import("com.joypard.workflow.definition.DefinitionDI");

K
		.defView(
				"com.joypard.workflow.definition.DefinitionView",
				{

					render : function() {

						this.definitionGridPanel = this
								.createDefinitionGridPanel();

						this.MP = new K.Ext.MainPanel({
							items : [ this.definitionGridPanel ]
						});
					},

					initialize : function() {
						E.addEvents("delDefinitionEvent", "startProcessEvent");

						E.addListener("delDefinitionSuccessEvent",
								this.onDelDefinitionSuccess, this);
					},

					createDefinitionGridPanel : function() {
						var me = this;
						var sm = new Ext.grid.CheckboxSelectionModel({
							singleSelect : true
						});
						var options = {
							title : "流程定义列表",
							iconCls : "p-grid",
							margins : "2 2 2 2",
							region : "center",
							viewConfig : {
								forceFit : true
							},
							store : DefinitionDI.createDefinitionStore(),
							tbar : [
									{
										text : "部署流程",
										iconCls : 'add',
										handler : K.createDelegate(
												this.onDeployment, this)
									},
									{
										text : "删除",
										iconCls : 'delete',
										handler : K.createDelegate(
												this.onDelDeployment, this)
									} ],
							sm : sm,
							cm : new Ext.grid.ColumnModel([ sm, {
								header : "名称",
								width : 2,
								dataIndex : "name"
							}, {
								header : "标识",
								width : 1,
								dataIndex : "key"
							}, {
								header : "ID",
								width : 1,
								dataIndex : "id"
							}, {
								header : "版本",
								width : 1,
								dataIndex : "version"
							}, {
								header : "资源名称",
								width : 4,
								dataIndex : "resourceName"
							}, {
								header : "部署ID",
								width : 1,
								dataIndex : "deploymentId"
							}, {
								header : "启动",
								width : 2,
								dataIndex : "id",
								renderer : function(v,m,r) {
									var btnId = Ext.id();// 获得Ext创建的唯一id
									createGridButton.defer(1, this, [ btnId ]);
									function createGridButton() {
										return new Ext.Button({
											text : '启动',
											handler : function() {
												me.onStartProcess(v);
											}
										}).render(document.body, btnId);
									}
									return "<div id=" + btnId + "></div>";
								}
							} ]),
							bbar : {
								xtype : "pagination"
							},
							listeners : {
								afterrender : function() {
									me.definitionGridPanel.getStore().load();
								}
							}
						};

						return new K.Ext.grid.GridPanel(options);
					},

					deployWin : null,

					createDeployWin : function() {
						var me = this;
						var options = {
							width : 400,
							title : '部署流程定义',
							height : 200,
							closeAction : 'hide',
							buttons : [
									{
										text : '确定',
										iconCls : "w-save",
										handler : function() {
											var uploadForm = $(
													"#uploadDeployForm",
													me.deployWin.getEl().dom);
											uploadForm.submit();
										}
									}, {
										text : '关闭',
										iconCls : "w-cancel",
										handler : function() {
											me.deployWin.hide();
										}
									} ]
						};
						return new K.Ext.Window(options);
					},

					onDeployment : function() {
						var me = this;
						if (!me.deployWin) {
							me.deployWin = me.createDeployWin();
						}
						me.deployWin.show();
						me.deployWin.load({
							url : DefinitionDI.urls.upload,
							scripts : true
						});
					},

					onDelDeployment : function() {
						var records = this.definitionGridPanel
								.getSelectionModel().getSelections();
						if (records.length > 0) {
							var msg = [ "删除的流程定义有:{num}条" ];
							msg
									.push("---------------------------------------------------------------------------")
							msg.push(K.arrayToStr(records, function(r) {
								return r.get("deploymentId")
							}, ", "));
							msg
									.push("---------------------------------------------------------------------------")
							msg.push("您确定要删除吗?");
							msg = K.format(msg.join("\n"), {
								num : records.length
							});
							if (!confirm(msg)) {
								return;
							}
							E.fireEvent("delDefinitionEvent", records);
						} else {
							K.err("请选择流程定义", this.definitionGridPanel.tbar);
						}
					},

					onDelDefinitionSuccess : function() {
						K.m("删除成功").show(this.definitionGridPanel.tbar);
						this.definitionGridPanel.getStore().reload();
					},

					onStartProcess : function(processDefinitionId) {
						E.fireEvent("startProcessEvent", processDefinitionId);
					}

				});
